package checkauto.camera.com.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public final class ViewfinderView extends View {
    private int checkLeftFrame = 0;// 检测证件左边是否存在或者对齐
    private int checkTopFrame = 0;// 检测证件上边是否存在或者对齐
    private int checkRightFrame = 0;// 检测证件右边是否存在或者对齐
    private int checkBottomFrame = 0;// 检测证件下边是否存在或者对齐
    private static final int[] SCANNER_ALPHA = { 0, 64, 128, 192, 255, 192,
            128, 64 };
    /**
     * 刷新界面的时间
     */
    private static final long ANIMATION_DELAY = 50L;

    public int getCheckLeftFrame() {
        return checkLeftFrame;
    }

    public void setCheckLeftFrame(int checkLeftFrame) {
        this.checkLeftFrame = checkLeftFrame;
    }

    public int getCheckTopFrame() {
        return checkTopFrame;
    }

    public void setCheckTopFrame(int checkTopFrame) {
        this.checkTopFrame = checkTopFrame;
    }

    public int getCheckRightFrame() {
        return checkRightFrame;
    }

    public void setCheckRightFrame(int checkRightFrame) {
        this.checkRightFrame = checkRightFrame;
    }

    public int getCheckBottomFrame() {
        return checkBottomFrame;
    }

    public void setCheckBottomFrame(int checkBottomFrame) {
        this.checkBottomFrame = checkBottomFrame;
    }

    private static final int OPAQUE = 0xFF;
    private final Paint paint;
    private int scannerAlpha;
    /**
     * 中间滑动线的最顶端位置
     */
    private int slideTop;
    private int slideTop1;

    /**
     * 中间滑动线的最底端位置
     */
    private int slideBottom;
    /**
     * 中间那条线每次刷新移动的距离
     */
    private static final int SPEEN_DISTANCE = 10;
    /**
     * 扫描框中的线的宽度
     */
    private static final int MIDDLE_LINE_WIDTH = 6;
    private boolean isFirst = false;
    /**
     * 四周边框的宽度
     */
    private static final int FRAME_LINE_WIDTH = 4;
    private Rect frame;
    private int width;
    private int height;

    public ViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        paint = new Paint();
        Resources resources = getResources();
        scannerAlpha = 0;
    }

    @Override
    public void onDraw(Canvas canvas) {
        width = canvas.getWidth();
        height = canvas.getHeight();
       frame = new Rect((int) (width * 0.15),
                (int) (height - 0.41004673 * width) / 2, (int) (width * 0.8),
                (int) (height + 0.41004673 * width) / 2);
        if (frame == null) {
            return;
        }
        // 初始化中间线滑动的最上边和最下边
        if (!isFirst) {
            isFirst = true;
            slideTop = width / 3;
            slideBottom = 2 * width / 3;
            slideTop1 = height / 3;
        }

        // 画出扫描框外面的阴影部分，共四个部分，扫描框的上面到屏幕上面，扫描框的下面到屏幕下面
        // 扫描框的左边面到屏幕左边，扫描框的右边到屏幕右边
        paint.setColor(Color.argb(128, 0, 0, 0));
        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1,
                paint);
        canvas.drawRect(0, frame.bottom + 1, width, height, paint);

        // 绘制两个像素边宽的绿色线框
        paint.setColor(Color.rgb(0, 255, 0));
        canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.top,
                frame.left + FRAME_LINE_WIDTH - 2 + 50, frame.top
                        + FRAME_LINE_WIDTH, paint);
        canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.top,
                frame.left + FRAME_LINE_WIDTH + 2, frame.top + 50, paint);// 左上角
        canvas.drawRect(frame.right - FRAME_LINE_WIDTH - 2, frame.top,
                frame.right - FRAME_LINE_WIDTH + 2, frame.top + 50, paint);
        canvas.drawRect(frame.right - FRAME_LINE_WIDTH - 2 - 50, frame.top,
                frame.right - FRAME_LINE_WIDTH + 2, frame.top
                        + FRAME_LINE_WIDTH, paint);// 右上角
        canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.bottom - 50,
                frame.left + FRAME_LINE_WIDTH + 2, frame.bottom, paint);
        canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.bottom
                - FRAME_LINE_WIDTH, frame.left + FRAME_LINE_WIDTH - 2 + 50,
                frame.bottom, paint); // 左下角
        canvas.drawRect(frame.right - FRAME_LINE_WIDTH - 2, frame.bottom - 50,
                frame.right - FRAME_LINE_WIDTH + 2, frame.bottom, paint);
        canvas.drawRect(frame.right - FRAME_LINE_WIDTH - 2 - 50, frame.bottom
                - FRAME_LINE_WIDTH, frame.right - FRAME_LINE_WIDTH - 2,
                frame.bottom, paint); // 右下角
        // 如果检测到证件左边就会画出左边的提示线
        if (checkLeftFrame == 1)
            canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.top,
                    frame.left + FRAME_LINE_WIDTH + 2, frame.bottom, paint);// 左边
        // 如果检测到证件上边就会画出左边的提示线
        if (checkTopFrame == 1)
            canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.top,
                    frame.right - FRAME_LINE_WIDTH + 2, frame.top
                            + FRAME_LINE_WIDTH, paint);// 上边
        // 如果检测到证件右边就会画出左边的提示线
        if (checkRightFrame == 1)
            canvas.drawRect(frame.right - FRAME_LINE_WIDTH - 2, frame.top,
                    frame.right - FRAME_LINE_WIDTH + 2, frame.bottom, paint);// 右边
        // 如果检测到证件底边就会画出左边的提示线
        if (checkBottomFrame == 1)
            canvas.drawRect(frame.left + FRAME_LINE_WIDTH - 2, frame.bottom
                    - FRAME_LINE_WIDTH, frame.right - FRAME_LINE_WIDTH - 2,
                    frame.bottom, paint); // 右下角

        /**
         * 当我们获得结果的时候，我们更新整个屏幕的内容
         */

         postInvalidateDelayed(ANIMATION_DELAY, 0, 0, width, height);

    }
}
